<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-select v-model="listQuery.proxy" placeholder="请选择代理账号" clearable filterable style="width: 150px" class="filter-item" @change="handleFilter">
        <el-option v-for="(item,index) in dailiArr" :key="index" :label="item" :value="item" />
      </el-select> -->
      <el-date-picker
        v-model="value1"
        align="left"
        unlink-panels
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        class="filter-item pickertime"
        format="yyyy-MM-dd"
        value-format="timestamp"
        :picker-options="pickerOptions2"
        size="small"
        @change="handleFilter"
      />
      <el-button v-waves plain size="small" class="filter-item" type="primary" @click="handleFilter">
        搜索
      </el-button>
      <!-- <el-button v-waves plain size="small" class="filter-item" type="primary" icon="el-icon-refresh" @click="handleCancel">
        重置
      </el-button> -->
      <span style="margin-left: 20px" />
      <!-- <template>
        <el-radio-group v-model="listQuery.status" @change="handleFilter">
          <el-radio label="">全部</el-radio>
          <el-radio :label="1">已结算</el-radio>
          <el-radio :label="2">未结算</el-radio>
          <el-radio :label="3">已作废</el-radio>
        </el-radio-group>
      </template> -->
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      fit
      :summary-method="getSummaries"
      highlight-current-row
      style="width: 100%;"
    >
      <!-- <el-table-column label="日期" align="center" min-width="100">
        <template slot-scope="{row}">
          <span>{{ row.time_ }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="用户" min-width="80" align="center">
        <template slot-scope="{row}">
          <span>{{ row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算日期" align="center">
        <template slot-scope="{row}">
          <span>{{ row.period }}</span>
        </template>
      </el-table-column>
      <el-table-column label="UV" min-width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.uv }}</span>
        </template>
      </el-table-column>
      <el-table-column label="PV" min-width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.pv }}</span>
        </template>
      </el-table-column>
      <el-table-column label="万次UV单价" min-width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金额" min-width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="80" align="center">
        <template slot-scope="{row}">
          <!-- <el-tag :type="row.status | statusFilter"> -->
            {{ allStatus[row.status] }}
          <!-- </el-tag> -->
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" min-width="180" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button :type="row.status!=2? 'info':'primary'" :disabled="row.status!=2" size="mini" @click="handleSettle(row,'结算')">
            结算
          </el-button>
          <el-button size="mini" :type="row.status!=2? 'info':'danger'" :disabled="row.status!=2" @click="handleSettle(row,'作废')">
            作废
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total > 0" :total="total" class="pageShow" :page.sync="listQuery.page" :limit.sync="listQuery.page_size" @pagination="getList" />
    <pagination v-show="total > 0" layout="prev, pager, next" :small="true" class="pageHidden" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.page_size" @pagination="getList" />
  </div>
</template>

<script>

import { getEarnings,getProxyNameList,changeSettleStatus } from '@/api/admin/admin'


import { fetchList, settleMoney, del } from '@/api/admin/settle'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { MessageBox, Message } from 'element-ui'

export default {
  name: 'earnings',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'danger',
        2: 'info'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      value1: '',
      countPv: '',
      countUv: '',
      countProfit: '',
      allStatus: {
        1: '已结算',
        2: '未结算',
        3: '已作废'
      },
      dailiArr: [],
      pickerOptions2: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      listQuery: {
        page: 1,
        page_size: 20, 
        proxy:'',
        time_start:'',
        time_end:'',
        status:''
      }
    }
  },
  created() {
    this.getProxyNameList();
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true
      if (this.value1) {
        this.listQuery.time_start = (this.value1[0]/1000)
        this.listQuery.time_end = (this.value1[1]/1000)
      } else {
        this.listQuery.time_start = ''
        this.listQuery.time_end = ''
      }
      getEarnings(this.listQuery).then(response => {
        this.list = response.DATA.list
        this.total = response.DATA.total 
        this.countPv = response.DATA.sum.pv
        this.countUv = response.DATA.sum.uv
        this.countProfit = response.DATA.sum.money
        this.listLoading = false
      })
    },

      //获取代理列表
    getProxyNameList() {
      getProxyNameList().then((r) => {
        this.dailiArr = r.DATA;
      });
    },
    getSummaries() {
      const sums = ['总计', '-', '-', this.countUv, this.countPv, '-', this.countProfit, '-', '-']
      return sums
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCancel() {
      this.value1 = ''
      this.listQuery.page = 1
      this.listQuery.proxy = ''
      this.listQuery.status = ''
      this.getList()
    },
    // handleSettle(row,title) {
    //   MessageBox.confirm(`确定${title}吗？`, '确认', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(() => {
    //     let obj = {
    //       id:row.id,
    //       status: (title == '结算' ? 1 : 3),
    //     }
        
    //     changeSettleStatus(obj).then(response => {
    //       if (response.STATUS === 200) {
    //         this.getList()
    //         Message({
    //           message: response.msg || `${title}成功`,
    //           type: 'success',
    //           duration: 2000
    //         })
    //       } else {
    //         Message({
    //           message: response.msg ||  `${title}成功`,
    //           type: 'error',
    //           duration: 2000
    //         })
    //       }
    //     })
    //   }).catch(() => {})
    // }
  }
}
</script>

<style scoped>
  .pageHidden{
    display: none;
  }
  @media (max-width:550px) {
    .pageShow {
      display: none;
    }
    .pageHidden {
      display: block;
    }
    .pickertime{
      width: 100%;
    }
  }
</style>
